package com.hy.admin.controller.pages;

import com.hy.admin.entity.MemberEntity;
import com.hy.admin.helper.response.*;
import com.hy.admin.security.service.business.SecurityCurrentUserLoader;
import com.hy.admin.service.*;
import com.hy.core.response.ServicesResult;
import org.apache.commons.httpclient.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpSession;


/**
 * Created by yangqq on 18/4/3.
 * 个人中心
 */
@Controller
@RequestMapping("/page/course")
public class PageControllerCourse extends PageControllerBase{

    @Autowired
    private CourseService courseService;

    @Autowired
    private QuestionService questionService;
    @Autowired
    private AnswerService answerService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private AccountService accountService;

    @RequestMapping(value={"detail" }, method=RequestMethod.GET)
    public String introduce(Model model, Long id) {

        saveDefaultAttributes2Model(model);
        ServicesResult<CourseResponse> servicesResult = courseService.detail(id, null);
        if(servicesResult.getStatusCode() == HttpStatus.SC_OK && servicesResult.getResult() != null) {
                model.addAttribute("course", servicesResult.getResult());
        }
        return makeViewName("detail");
    }

    @RequestMapping(value={"teacher" }, method=RequestMethod.GET)
    public String teacher(Model model,Long id) {
        saveDefaultAttributes2Model(model);
        ServicesResult<PersonalResponse> servicesResult = teacherService.home(id);
        if(servicesResult.getStatusCode() == HttpStatus.SC_OK && servicesResult.getResult() != null) {
            model.addAttribute("teacher", servicesResult.getResult());
        }
        return makeViewName("teacher");
    }


    @RequestMapping(value={"list" }, method=RequestMethod.GET)
    public String list(Model model, Long id, HttpSession session) {
        saveDefaultAttributes2Model(model);
        ServicesResult<CourseResponse> servicesResult;

        SecurityCurrentUserLoader securityCurrentUserLoader=new SecurityCurrentUserLoader();
        MemberEntity memberEntity=securityCurrentUserLoader.load();

        if(memberEntity!=null){
            model.addAttribute("isLogin", 0);
            model.addAttribute("memberId", memberEntity.getId());
            servicesResult = courseService.detail(id, memberEntity.getId());
        }else {
            model.addAttribute("isLogin", 1);
            servicesResult = courseService.detail(id, null);
            model.addAttribute("memberId", "");
        }
        if(servicesResult.getStatusCode() == HttpStatus.SC_OK && servicesResult.getResult() != null) {
            model.addAttribute("course", servicesResult.getResult());
            model.addAttribute("isBuy", servicesResult.getResult().getIsBuy() ? 0 : 1);
            int commentCount = commentService.commentCount(id);
            model.addAttribute("commentCount", commentCount);
        }
        return makeViewName("list");
    }


    @RequestMapping(value={"question" }, method=RequestMethod.GET)
    public String question(Model model, Long id, HttpSession session) {
        saveDefaultAttributes2Model(model);
        ServicesResult<QuestionResponse> questionResult;
        SecurityCurrentUserLoader securityCurrentUserLoader=new SecurityCurrentUserLoader();
        MemberEntity memberEntity=securityCurrentUserLoader.load();

        if(memberEntity != null) {
            model.addAttribute("isLogin", 0);
//            MemberEntity memberEntity = (MemberEntity)session.getAttribute("memberEntity");
            model.addAttribute("memberId", memberEntity.getId());
            //查询问题详情
            questionResult = questionService.detail(memberEntity.getId(), id);
        }else{
            model.addAttribute("isLogin", 1);
            //查询问题详情
            questionResult = questionService.detail(null, id);
            model.addAttribute("memberId", "");
        }
        if(questionResult.getStatusCode() == HttpStatus.SC_OK && questionResult.getResult() != null) {
            model.addAttribute("question", questionResult.getResult().getQuestion());
            Boolean isPay = questionResult.getResult().getIsPay() == null ? false : questionResult.getResult().getIsPay();
            model.addAttribute("isPay", isPay ? 0 : 1);
        }
        //查询答案
        ServicesResult<AnswerResponse> answerResult = answerService.show(id,null);
        if(answerResult.getStatusCode() == HttpStatus.SC_OK && answerResult.getResult() != null) {
            model.addAttribute("answer", answerResult.getResult());
        }
        return makeViewName("question");
    }

//    @RequestMapping(value={"news" }, method=RequestMethod.GET)
//    public String news(Model model, Long id) {
//        saveDefaultAttributes2Model(model);
//        ServicesResult<NewsResponse> servicesResult = newsService.detail(id);
//        if(servicesResult.getStatusCode() == HttpStatus.SC_OK && servicesResult.getResult() != null) {
//            model.addAttribute("news", servicesResult.getResult().getNews());
//            model.addAttribute("imgList", servicesResult.getResult().getImgList());
//        }
//        return makeViewName("news");
//    }

    @Override
    protected String getViewPrefix() {
        return "course/";
    }
}
